# Object data reduction
#
# Use "setintrument ccd1k|ccd2k" to set default parameters for ccdproc and dispcor
#
# Created by Cesar Strauss
# Last modified in 06/03/2002 by Cesar Strauss: added comments
# To do: implement tasks datacube

procedure obj (image)

string image {"N6565_0001",prompt="Input CCD image"}
string trref {"fla0001",prompt="Fiber transmission calibration - unmasked flat"}
string imref {"near0001.lin",prompt="Calibration arc reference"}
string apref {"mask",prompt="Fiber apertures reference"}
bool   extract {"yes",prompt="Extract fibers ? (yes/no)"}
bool   ql    {"yes",prompt="Quick look ? (yes/no)"}
bool   tcor  {"yes",prompt="Transmission correction? (yes/no)"}
bool   dcor  {"yes",prompt="Dispersion correction? (yes/no)"}

pset ccdproc {prompt="ccdproc parameters (use :e to edit)"}
pset dispcor {prompt="dispcor parameters (use :e to edit)"}

begin

string image_file,fiber_file,ref_file,ap_file
string n_trref
bool n_ql, n_dcor, n_tcor

image_file = image
ref_file = imref
ap_file = apref
n_trref = trref

n_ql = ql
n_dcor = dcor
n_tcor = tcor

# Trim the image
# Apply zero level and overscan corrections
print("Trimming and applying zero level and overscan corrections...")
ccdproc (image_file)

# Linear gaussian extraction
if (extract == yes ) {
	if ( ql == yes ) {
	   print("Extracting apertures from input image...")
	   fiber_file = image_file // ".ql"
	   apsum(image_file,output=fiber_file,reference=ap_file) 
	}
	else {
	   print("Extracting gaussians from input image...")
	   fiber_file = image_file // ".lin"
	   fibint(input=image_file)
	}
}

# Transmission correction
if ( n_tcor == yes ) {
   transcor(input=fiber_file,output=fiber_file//".tc",imcor=n_trref)
   fiber_file = fiber_file // ".tc" 
}

# Wavelenght calibration
if ( n_dcor == yes ) {
   print ("Wavelenght calibration...")
   hedit (fiber_file,"REFSPEC1",ref_file,add+,upd+,del-,ver-,show-)
   dispcor(fiber_file,fiber_file//".wl")
   fiber_file = fiber_file // ".wl"
}

# Datacube conversion
print("Generating datacube...")
datacube(fiber_file,fiber_file//".dc")

end
